package Screens.MyScreens;

import java.lang.reflect.InvocationTargetException;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;

import Game.GameHelper;
import Screens.Manager.Screen;
import Screens.Manager.ScreenManager;
import Tools.Configuration;
import Tools.ResourceManager;

/** Intro screen class
 * @author Aikidos
 */
public class IntroScreen extends Screen {

	/** Intro image */
	private Image _intro = null;
	
	/** Alpha for intro image */
	private float _alpha = 0.f;

	/** Timer for update alpha */
	private int _timer;

	/** Reduce alpha? */
	private boolean _reduceAlpha = false;
	
	@Override
	public void init() throws SlickException, InstantiationException,
			IllegalAccessException, IllegalArgumentException,
			InvocationTargetException, NoSuchMethodException,
			SecurityException, ClassNotFoundException {
		
		GameHelper.getGame().getGraphics().setBackground(new Color(255, 255, 250));
		
		// Play music
		if (!Configuration.isMute())
			((Music) ResourceManager.getPool().get("music_MainTheme")).loop();
	}

	@Override
	public void loadContent() throws SlickException {
		_intro = new Image("res/gfx/aik.png");
		_intro.setAlpha(0);
		
		// Load music
		if (!ResourceManager.getPool().containsKey("music_MainTheme"))
			ResourceManager.getPool().put("music_MainTheme", new Music("res/music/theme.ogg", true));
	}

	@Override
	public void update(int delta) throws SlickException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		_intro.setAlpha(_alpha);
		
		if (_timer > 200) {
			_timer = 0;
			
			_alpha += _reduceAlpha ? -0.1 : 0.1;
			if (!_reduceAlpha)
				_reduceAlpha = !_reduceAlpha && _alpha >= 1.f;
			
		} else _timer += delta;
		
		// Control
		Input input = GameHelper.getGame().getInput();
		if (input.isKeyPressed(Input.KEY_ESCAPE)) _alpha = -0.1f;
		if (input.isKeyPressed(Input.KEY_RETURN)) _alpha = -0.1f;
		if (input.isKeyPressed(Input.KEY_SPACE)) _alpha = -0.1f;
		
		if (_alpha < 0) ScreenManager.go("MenuScreen");
	}

	@Override
	public void draw(Graphics g) throws SlickException {
		_intro.draw((GameHelper.getGame().getWidth() / 2) - (_intro.getWidth() / 2) ,
				(GameHelper.getGame().getHeight() / 2) - (_intro.getHeight() / 2));
	}

	@Override
	public void unloadContent() throws SlickException {
		_intro.destroy();
		_intro = null;
	}

	@Override
	public void shutdown() {
		
	}

}
